package gen.tools.role;

import gen.tools.Widgets;
import gen.tools.structures.I1S2;
import gen.tools.structures.RoleAssign;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryEmpsWithRole extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryEmpsWithRole() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		String subsysId = request.getParameter("subsysId");
		int roleId = Integer.parseInt( request.getParameter("roleId") );
		int systemId = Integer.parseInt( request.getParameter("systemId") );
		int polyType = Integer.parseInt( request.getParameter("polyType") );
		
		
		ArrayList<I1S2> in = new ArrayList<I1S2>();		//擁有 指定子系統下，擁有指定角色的人員
		ArrayList<I1S2> out = new ArrayList<I1S2>();		//非上述的其它人
		RoleAssign ra = new RoleAssign();
		
		String sql = "";
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = wid.getConn();
			String envId = "";
			
			//所有子系統, 環境下擁有指定角色的人
			if ( polyType ==1 ) {
				sql = "SELECT t1.empId,t2.empName,t2.sysSN FROM mpemprolesubsys t1,emps t2 WHERE t1.sysSN=t2.sysSN AND t1.empId=t2.empId AND t1.subsysId=? AND t1.roleId=? AND t2.sysSN=?";	
			} else {
				envId = request.getParameter("envId");
				sql = "SELECT t1.empId,t2.empName,t2.sysSN FROM mpemprolesubsysenv t1,emps t2 WHERE t1.sysSN=t2.sysSN AND t1.empId=t2.empId AND t1.subsysId=? AND t1.roleId=? AND t2.sysSN=? AND t1.envId=?";
			}
			
			ps = conn.prepareStatement( sql );
			int i=1;
			ps.setString(i++, subsysId);
			ps.setInt(i++, roleId);
			ps.setInt(i++, systemId);
			if ( polyType != 1 ) {
				ps.setString(i++, envId );
			}
			rs = ps.executeQuery();
			while ( rs.next() ) {
				I1S2 i1s2 = new I1S2();
				i1s2.setS1( rs.getString("empId") );
				i1s2.setS2( rs.getString("empName") );
				i1s2.setI1( rs.getInt("sysSN") );
				in.add(i1s2);
			}
			if ( rs !=null ) {rs.close();}
			if ( ps !=null ) {ps.close();}
			
			//未擁有此角色的人
			if ( polyType ==1 ) {
				sql = "SELECT t1.empId,t1.empName,t1.sysSN FROM emps t1 WHERE t1.sysSN=? AND t1.empId NOT IN (SELECT t2.empId FROM mpemprolesubsys t2 WHERE t2.subsysId=? AND t2.roleId=? AND t1.sysSN=t2.sysSN )";	
			} else {
				sql = "SELECT t1.empId,t1.empName,t1.sysSN FROM emps t1 WHERE t1.sysSN=? AND t1.empId NOT IN (SELECT t2.empId FROM mpemprolesubsysenv t2 WHERE t2.subsysId=? AND t2.roleId=? AND t1.sysSN=t2.sysSN AND t2.envId=? )";
			}
			
			ps = conn.prepareStatement( sql );
			i=1;
			ps.setInt(i++, systemId);
			ps.setString(i++, subsysId);
			ps.setInt(i++, roleId);
			if ( polyType != 1 ) {
				ps.setString(i++, envId );
			}
			rs = ps.executeQuery();
			
			while ( rs.next() ) {
				I1S2 i1s2 = new I1S2();
				i1s2.setS1( rs.getString("empId") );
				i1s2.setS2( rs.getString("empName") );
				i1s2.setI1( rs.getInt("sysSN") );
				out.add(i1s2); 
			}
		
		} catch (Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch (Exception e) { e.printStackTrace(); };
			
		}
		
		ra.setInList(in);
		ra.setOutList(out);
		
		response.getWriter().write( new Gson().toJson( ra ) );
	}

}
